package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.asymmetric.RSA;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyFujianBeiYinJinRongData;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 * 福建北银金融
 * @author 李源聪
 */
@ApiPlan(productId = "16", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5FujianBeiYinJinRong implements ApiOrgPhoneMd5ConvertHandler {

    private final String CHANNEL_CODE = "anxinfenqifuzhou";
    private String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnt6FGUwXpB3VW4ZupN4Waeo2qKmCNLZFETVRK3iPkhPathHfuKRygQCz2xRkGWoPBt25K8Q3ReCLPuiH4jDyDSN-03tRXHXWtMRVho0TtOrYp2qfJVhDhoA4rFL10pK0VflBhbpfke4n4o1-D4Q6FMr5ZEJIzYutUaySQaoHT0U2V6JRStWGX7Ec3bkRoiRnfzNATk0FZadTJMcwZ5qvgOe7WwRo8PDktm2hQjKqaI66eDgZUzD3RhVkN9XNlhbpSsMoqP34VMP1JEoXxMnXMgDS8YqljHgu7GmLFWu7p-vZB0jp-0dlAT_VfXsuJI73e4cxdbqJZFpT_MGx_VhMWQIDAQAB";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelCode", CHANNEL_CODE);
            newMap.put("phone", apiDataExchangeParameter.getPhoneMd5());
        });

        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {

        RSA rsa = new RSA(publicKey, null);

        OrgApplyFujianBeiYinJinRongData orgApplyFujianBeiYinJinRongData = new OrgApplyFujianBeiYinJinRongData();
        // 渠道编号
        orgApplyFujianBeiYinJinRongData.setChannelCode(CHANNEL_CODE);
        // 手机号（RSA加密后）
        String phone = apiApplyParameter.getPhone();
        if (!StringUtils.isEmpty(phone)) {
            orgApplyFujianBeiYinJinRongData.setPhone(rsa.encryptToBase64Str(phone));
        }

        // 身份证号码(RSA加密后)
        String idcard = apiApplyParameter.getIdcard();
        if (!StringUtils.isEmpty(idcard)) {
            orgApplyFujianBeiYinJinRongData.setIdCardNo(rsa.encryptToBase64Str(idcard));
        }
        // 姓名(RSA加密后)
        String userName = apiApplyParameter.getUserName();
        if (!StringUtils.isEmpty(userName)) {
            orgApplyFujianBeiYinJinRongData.setName(rsa.encryptToBase64Str(userName));
        }
        // 贷款额度/单位:万元
        Integer loanAmountChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end();
        orgApplyFujianBeiYinJinRongData.setLoanAmount(loanAmountChoose);
        // 借款期限1.1个月2.3个月3.6个月4.9个月5.12个月6.24个月 7.36个月8.36个月以上
        orgApplyFujianBeiYinJinRongData.setLoanLimit("1");
        // 借款用途1.购车贷款2.购房贷款3.装修贷款4.教育贷款5.消费贷款 6.过桥贷款7.结婚贷款8.旅游贷款9.医疗贷款10.其他贷款
        String loanUse = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", "8")
                .when(apiApplyParameter.getLoanPurpose(), "2", "4")
                .when(apiApplyParameter.getLoanPurpose(), "3", "7")
                .when(apiApplyParameter.getLoanPurpose(), "4", "3")
                .when(apiApplyParameter.getLoanPurpose(), "5", "4")
                .when(apiApplyParameter.getLoanPurpose(), "6", "10")
                .end();
        orgApplyFujianBeiYinJinRongData.setLoanUse(loanUse);
        //性别1:男2:女3:未知
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", "2")
                .when(apiApplyParameter.getSex(), "0", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setSex(sex);
        // 年龄
        orgApplyFujianBeiYinJinRongData.setAge(Integer.valueOf(apiApplyParameter.getAge()));
        // 工作城市,结尾不要带‘市’
        orgApplyFujianBeiYinJinRongData.setCityOfWork(apiApplyParameter.getCity().replace("市",""));
        // 工作城市代码，重庆：500100
        orgApplyFujianBeiYinJinRongData.setCityOfWorkCode(Integer.valueOf(apiApplyParameter.getAreaCode()));
        // 月收入/元
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 3000)
                .when(apiApplyParameter.getMonthlySalary(), "2", 8000)
                .when(apiApplyParameter.getMonthlySalary(), "3", 15000)
                .when(apiApplyParameter.getMonthlySalary(), "4", 30000)
                .when(apiApplyParameter.getMonthlySalary(), "5", 50000)
                .end();
        orgApplyFujianBeiYinJinRongData.setRevenue(monthlySalary);
        // 职业0.无固定职业1.工薪族2.生意人
        String profession = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "1")
                .when(apiApplyParameter.getCareer(), "2", "0")
                .when(apiApplyParameter.getCareer(), "3", "2")
                .when(apiApplyParameter.getCareer(), "4", "1")
                .when(apiApplyParameter.getCareer(), "5", "0")
                .end();
        orgApplyFujianBeiYinJinRongData.setProfession(profession);
        // 公积金0.无1.有
        String reservedFunds = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "0")
                .when(apiApplyParameter.getProvidentFund(), "2", "1")
                .when(apiApplyParameter.getProvidentFund(), "3", "1")
                .when(apiApplyParameter.getProvidentFund(), "4", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setReservedFunds(reservedFunds);
        // 社保0.无1.有
        String socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "0")
                .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                .when(apiApplyParameter.getSocialSecurity(), "3", "1")
                .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setSocialSecurity(socialSecurity);
        // 信用卡0.无1.有
        String creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", "0")
                .when(apiApplyParameter.getCreditLimit(), "2", "1")
                .when(apiApplyParameter.getCreditLimit(), "3", "1")
                .when(apiApplyParameter.getCreditLimit(), "4", "1")
                .when(apiApplyParameter.getCreditLimit(), "5", "1")
                .when(apiApplyParameter.getCreditLimit(), "6", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setCreditCard(creditCard);
        // 芝麻分0.无1.550分以下2.550-600分3.600-650分4.650-700分 5.700分及以上
        String zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", "2")
                .when(apiApplyParameter.getSesameCredit(), "2", "3")
                .when(apiApplyParameter.getSesameCredit(), "3", "4")
                .when(apiApplyParameter.getSesameCredit(), "4", "5")
                .end();
        orgApplyFujianBeiYinJinRongData.setZhima(zhima);
        // 车产0.无1.有
        String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "0")
                .when(apiApplyParameter.getCarInfo(), "2", "1")
                .when(apiApplyParameter.getCarInfo(), "3", "1")
                .when(apiApplyParameter.getCarInfo(), "4", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setCar(car);
        // 房产0.无1.本地房2.外地房
        String house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "0")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setHouse(house);
        // 寿险保单0.无1.有
        String insurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "0")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "1")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "1")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setInsurance(insurance);
        // 学历0.高中及以下1.大专2.本科3.本科以上
        String education = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", "0")
                .when(apiApplyParameter.getEducationAttainment(), "2", "0")
                .when(apiApplyParameter.getEducationAttainment(), "3", "0")
                .when(apiApplyParameter.getEducationAttainment(), "4", "1")
                .when(apiApplyParameter.getEducationAttainment(), "5", "2")
                .when(apiApplyParameter.getEducationAttainment(), "6", "3")
                .end();
        orgApplyFujianBeiYinJinRongData.setEducation(education);
        // 工资代发0.无1.有
        String salary = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "1")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "0")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "0")
                .end();
        orgApplyFujianBeiYinJinRongData.setSalary(salary);
        // 微粒贷0.无1.有
        orgApplyFujianBeiYinJinRongData.setWeili("0");
        // 营业执照0.无1.有
        String businessLicense = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", "0")
                .when(apiApplyParameter.getBizLicenseYear(), "2", "1")
                .when(apiApplyParameter.getBizLicenseYear(), "3", "1")
                .when(apiApplyParameter.getBizLicenseYear(), "4", "1")
                .end();
        orgApplyFujianBeiYinJinRongData.setBusinessLicense(businessLicense);
        // 备注字段
        orgApplyFujianBeiYinJinRongData.setRemark(apiApplyParameter.getFormRemark());
        return orgApplyFujianBeiYinJinRongData;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
